//
//  RegisterNowFromViewController.m
//  Gxj
//
//  Created by 刘伟 on 2017/9/15.
//  Copyright © 2017年 wkq. All rights reserved.
//

#import "RegisterNowFromViewController.h"
#import "RegisterNowFromView.h"

@interface RegisterNowFromViewController ()<UITextFieldDelegate>
{
    RegisterNowFromView *_registerNowView;
    int timeTick;
    NSTimer* timer;
}

@end

@implementation RegisterNowFromViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _registerNowView = [[RegisterNowFromView alloc] init];
    _registerNowView.userNum.delegate = self;
    _registerNowView.userPassWord.delegate = self;
    _registerNowView.verification.delegate = self;
    
    [_registerNowView.loginNow addTarget:self action:@selector(clickLoginNow:) forControlEvents:UIControlEventTouchUpInside];
    [_registerNowView.registerBtn addTarget:self action:@selector(clickRegister:) forControlEvents:UIControlEventTouchUpInside];
    [_registerNowView.sendVerification addTarget:self action:@selector(clickSendVerification:) forControlEvents:UIControlEventTouchUpInside];
    self.view = _registerNowView;
}

// 注册
- (void)clickRegister:(UIButton *)sender
{
    NSDictionary * dicc =@{@"username":_registerNowView.userNum.text,
                           @"smscode":_registerNowView.verification.text,
                           @"password":_registerNowView.userPassWord.text
                           };
    
    [KQNetWork postWithURLString:[NSString stringWithFormat:@"%@/api/index/register",MAIN_URL] parameters:dicc success:^(NSDictionary *dic) {
        
        NSLog(@"userRegister:%@",dic);
        
        [SVProgressHUD showInfoWithStatus:[dic objectForKey:@"msg"]];
        
    } failure:^(NSError *error) {
        
    }];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField.tag == 200000) {
        if (range.location >= 11){
            return NO; // return NO to not change text
        }
    }else if (textField.tag == 200001){
        if (range.location >= 12) {
            return NO;
        }
    }else if (textField.tag == 200002){
        if (range.location >= 6) {
            return NO;
        }
    }
    
    return YES;
}

// 验证码倒计时
-(void)timeFireMethod
{
    timeTick--;
    if(timeTick==0){
        
        _registerNowView.sendVerification.enabled = YES;
        [timer invalidate];
        [_registerNowView.sendVerification setTitle:@"获取验证码" forState:0];
    }else
    {
        [_registerNowView.sendVerification setTitle:[NSString stringWithFormat:@"%ds后重新获取",timeTick] forState:0];
        
        [_registerNowView.sendVerification setTitleColor:[UIColor colorWithRed:66.0/255.0 green:167.0/255.0 blue:235.0/255.0 alpha:1] forState:0];
        _registerNowView.sendVerification.enabled = NO;
        _registerNowView.sendVerification.selected = NO;
    }
}

// 获取验证码
- (void)clickSendVerification:(UIButton *)sender
{
    BOOL isPhoneNumber = [Regula isMobilePhoneNumber:_registerNowView.userNum.text];
    
    if (isPhoneNumber == YES) {
        
        NSDictionary *dicc = @{@"username":_registerNowView.userNum.text
                               };
        
        [KQNetWork postWithURLString:[NSString stringWithFormat:@"%@/api/index/registercode",MAIN_URL] parameters:dicc success:^(NSDictionary *dic) {
            
            BOOL resstate = [dic objectForKey:@"resstate"];
            if (resstate == true) {
                // 发送成功
                [SVProgressHUD showSuccessWithStatus:[dic objectForKey:@"msg"]];
                timeTick =  60;
                timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES];
            }
            
            else{
                [SVProgressHUD showErrorWithStatus:[dic objectForKey:@"msg"]];
            }
            
        } failure:^(NSError *error) {
            [SVProgressHUD showErrorWithStatus:@"网络异常~\(≧▽≦)/~啦啦啦"];
        }];
    }
    else{
        [TTAlertView alertViewWithTitle:@"获取验证码失败" Message:@"请输入正确的手机号" cancelBtnTitle:nil sureBtnTitle:@"确定" sureBtnClick:^{
            
        } cancelBtnClick:^{
            
        }];
    }
    
}

// 立即登陆
- (void)clickLoginNow:(UIButton *)sender
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

#pragma mark - 点击空白回收键盘
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
